using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using System.Web.Mvc;
namespace Services
{

    public partial class CategoryArticleService
    {
       readonly ICategoryArticleEntity entity;
       readonly IArticleEntity article;
       public CategoryArticleService(ICategoryArticleEntity entity, IArticleEntity article)
       {
           this.entity = entity;
           this.article = article;

       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model)
       {
           try
           {
               var obj = ((CategoryArticle)_model);
               var id = obj.ID;
               if (id == 0)
               {

                   obj.Code = obj.Name;
                   if (obj.Link == null || obj.Link == "")
                   {
                       obj.Link = StringHelper.GeneratorLink(obj.Name);
                   }
                   if (obj.Image == null || obj.Image == "")
                   {
                       obj.Image = "noimage.png";
                   }
                   return entity.Save((CoreData.CategoryArticle)_model, Table.CategoryArticle.ToString());
               }
               else
               {
                   if (obj.Image == null || obj.Image == "")
                   {
                       obj.Image = GetByID(obj.ID).Image;
                   }
                   if (obj.Link == null || obj.Link == "")
                   {
                       obj.Link = StringHelper.GeneratorLink(obj.Name);
                   }
                   return entity.Update((CoreData.CategoryArticle)_model, Table.CategoryArticle.ToString());
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.CategoryArticle GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.CategoryArticle.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.CategoryArticle> GetList()
       {
           try
           {
               return entity.GetAll(Table.CategoryArticle.ToString()).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.CategoryArticle> GetListByLINQ(Func<CoreData.CategoryArticle, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.CategoryArticle.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.CategoryArticle GetOneByLINQ(Func<CoreData.CategoryArticle, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.CategoryArticle.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.CategoryArticle> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetBySearchString(_searchstring, Table.CategoryArticle.ToString()).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.CategoryArticle)_model, Table.CategoryArticle.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion



        #region Other Method

        public List<SelectListItem> GetListCategoryArticle(int id)
       {
           List<SelectListItem> lst = new List<SelectListItem>();
           var data = GetList();
            foreach(var item in data)
            {
                SelectListItem _item = new SelectListItem();
                _item.Text = item.Name;
                _item.Value = item.ID.ToString();
                if(id==item.ID)
                {
                    _item.Selected = true;
                }
                lst.Add(_item);
            }
            return lst;
        }


        public List<CategoryArticle> GetCategoryWithArticle()
        {
            List<CategoryArticle> lst = new List<CategoryArticle>();
            try
            {
                var data = GetListByLINQ(c => c.Active == true);
                foreach (var item in data)
                {
                    var art = article.GetMany(c => c.CateID == item.ID, Table.Article.ToString());
                    if (art != null && art.Count() != 0)
                    {
                        item.ArticleExt = art.ToList().OrderByDescending(c => c.UpdateDate).Take(5).ToList();
                    }
                    lst.Add(item);
                }
            }
            catch { }
            return lst;

        }


        #endregion 
    
    }
         
}
